﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;

namespace ESAM_Assignment_3
{
	public partial class EditComment : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				string fullname = "";
				string name = Request.QueryString["username"];
				bool query = CommentClass.checkValueQS(name);
				if (name == null)
				{
					Response.Redirect("Error.aspx");
				}
				else
				{
					fullname = UserClass.GetFullname(name);
				}
				if ((bool)Session[Common.ESAM_LOGIN] == false)
				{
					Response.Redirect("Default.aspx");
				}
				if ((string)Session[Common.ESAM_FULLNAME] != fullname)
				{
					Response.Redirect("Error.aspx");
				}

				int CommentID = Convert.ToInt32(Request.QueryString["CommentID"]);

				CommentClass myComment = CommentClass.GetComment(CommentID);

				StringBuilder sb = new StringBuilder(HttpUtility.HtmlDecode(myComment.Comment));

				//sb.Replace("&amp;#39;", "'");

				if (myComment != null)
				{
					tbxComments.Text = sb.ToString();
					tbxSubject.Text = myComment.Subject;
					lblDatePosted.Text = myComment.DatePosted;
					lblCommentID.Text = CommentID.ToString();
					lblFullname.Text = myComment.Fullname;
				}
			}
		}

		protected void btnUpdateComment_Click(object sender, EventArgs e)
		{
			StringBuilder sb = new StringBuilder(HttpUtility.HtmlEncode(tbxComments.Text));

			sb.Replace("&lt;b&gt;", "<b>");
			sb.Replace("&lt;/b&gt;", "</b>");
			sb.Replace("&lt;i&gt;", "<i>");
			sb.Replace("&lt;/i&gt;", "</i>");

			int CommentID = Convert.ToInt32(lblCommentID.Text);
			string comment = sb.ToString();
			string subject = tbxSubject.Text;
			string datePosted = Common.InsertTodayIntoCommentsDate();
			string name = Request.QueryString["username"];


			bool status = CommentClass.UpdateComment(CommentID, comment, datePosted, subject);

			if (status)
			{
				lblStatus.Text = "Comment updated successfully.";
				lblStatus.ForeColor = System.Drawing.Color.Blue;
			}
			else
			{
				lblStatus.Text = "Comment failed to update!!!.";
				lblStatus.ForeColor = System.Drawing.Color.Red;
			}
		}

	}
}